import { compare, isInArray } from "@/utils"
import { compact } from "lodash"

/* 爬虫处理任务模块 */
export default class CreepMissonWork extends Creep {
    public ManageMisson():void{
        if (!this.memory.role) return
        if (this.spawning) return
        if (!this.memory.MissonData) this.memory.MissonData = {}
        /* 生命状态监测 只针对transport和manage */
        if(this.ticksToLive < 10 && (isInArray(['transport','manage'],this.memory.role)))
        {
            var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.StructureIdData.storageID) as StructureStorage
            if (!storage_) return
            if (this.store.getUsedCapacity() > 0)
            {
                for (var i in this.store)
                {
                    this.transfer__(storage_,i as ResourceConstant)
                    return
                }
            }
            return
        }
        if (Object.keys(this.memory.MissonData).length <= 0)
        {
            /* 每任务的情况下考虑领任务 */
            if (!Game.rooms[this.memory.belong].memory.Misson['Creep'])
            Game.rooms[this.memory.belong].memory.Misson['Creep'] = []
            var taskList = Game.rooms[this.memory.belong].memory.Misson['Creep']
            var thisTaskList:MissionModel[] = []
            for (var Stask of taskList)
            {
            if (Stask.CreepBind && isInArray(Object.keys(Stask.CreepBind),this.memory.role))
                thisTaskList.push(Stask)
            }
            if (thisTaskList.length <= 0)
            {
                /* 没任务就处理剩余资源 */
                if (this.room.name != this.memory.belong) return
                var st = this.store
                if (!st) return
                for (var i of Object.keys(st))
                {
                    var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.StructureIdData.storageID) as StructureStorage
                    if (!storage_ ) return
                    this.say("🛒")
                    if (this.transfer(storage_,i as ResourceConstant) == ERR_NOT_IN_RANGE) this.goTo(storage_.pos,1)
                    return
                }
                return
            }
            else
            {
                /* 根据优先等级排列，领取最优先的任务 */
                thisTaskList.sort(compare('level'))
                /* 还没有绑定的任务，就等待接取任务 */
                LoopBind:
                for (var t of thisTaskList)
                {
                    if (t.CreepBind && t.CreepBind[this.memory.role] && t.CreepBind[this.memory.role].bind.length < t.CreepBind[this.memory.role].num)
                    {
                        /* 绑定任务了就输入任务数据 */
                        t.processing = true // 领取任务后，任务开始计时
                        t.CreepBind[this.memory.role].bind.push(this.name)
                        this.memory.MissonData.MisssonID = t.id           // 任务id
                        this.memory.MissonData.MissonName = t.name        // 任务名
                        this.memory.MissonData.MissonDelay = 1500  // 爬虫处理任务的超时时间
                        this.memory.MissonData.data = t.data?t.data:{}    // 任务数据传输
                        break LoopBind
                    }
                }
                if (Object.keys(this.memory.MissonData).length <= 0) this.say("找不到任务了!")
                return
            }
        }
        else
        {
            /* 有任务做任务 */
            this.memory.MissonData.MissonDelay --     // 爬虫内置Tick计时
            if (this.memory.MissonData.MissonDelay <= 0)
            {
                this.memory.MissonData = {}
                return
            }
            switch (this.memory.MissonData.MissonName) {
                case "虫卵填充":{ this.handle_task_feed();break;}
                case "物流运输":{ this.handle_task_carry();break;}  // 重要
                case '控制冲级':{ this.handle_task_level();break;}
                case "房间签名":{this.handle_scout();break;}
                case '援建一体':{this.handle_BoostBuild();break;}
                case '协助升级':{this.handle_HelpUpgrade();break;}
                case '后勤协助':{this.handle_HelpRear();break;}
                case '攻防一体':{this.handle_Aio();break;}
                case '原矿开采':{this.handle_mineral();break;}
                case 'power采集':{this.handle_power();break;}
                case '双人小队':{this.handle_task_double();break;}
                case '墙体维护':{this.handle_repair();break;}
                case 'deposit采集':{this.handle_deposit();break;}
                case '外矿开采':{this.handle_outmine();break;}
                case 'X-R':{this.handle_task_suqard();break;}
                case '主动防御':{this.handle_task_defend();break;}
                case '黄球拆迁':{this.handle_task_dismantle();break;}
                case '控制攻击':{this.handle_task_claimattack();break;}
                case 'om杀手':{this.handle_om_kill();break;}
            }

        }
    }
}